package com.tzbank.common.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.tzbank.common.dao.BaseMsgTemplateMapper;
import com.tzbank.common.service.BaseMsgTemplateService;
import com.tzbank.entity.BaseMsgTemplate;
import com.tzbank.utils.DateUtils;
import com.tzbank.utils.String2Utils;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class BaseMsgTemplateServiceImpl implements BaseMsgTemplateService {

	@Resource
	private BaseMsgTemplateMapper baseMsgTemplateMapper;

	@Override
	public Map<String, Object> addMsgTemplate(Map<String, Object> reqBody) {
		log.info("保存消息模板主体 请求参数:{}",JSON.toJSONString(reqBody));
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("success", true);
		result.put("info", "添加成功");
		
		String taskName = reqBody.get("taskName").toString();
		String title = reqBody.get("title").toString();
		String content = reqBody.get("content").toString();
		String uri = reqBody.get("uri").toString();
		String deliveryType = reqBody.get("deliveryType").toString();
		String expiredSeconds = reqBody.get("expiredSeconds").toString();
		String targetMsgkey = reqBody.get("targetMsgkey").toString();
		String pushDate =  reqBody.get("pushDate").toString();
		//String pushState = reqBody.get("pushState").toString();
		String msgType = reqBody.get("msgType").toString();
		String showMode = reqBody.get("showMode").toString();
		//String readStatus = reqBody.get("readStatus").toString();
		String pushTag = reqBody.get("pushTag").toString();
		String pushObject = reqBody.get("pushObject").toString();
		
		String remark = reqBody.get("remark").toString();
		String templateId = "MT"+String2Utils.getGenerateUUID();
		BaseMsgTemplate tem = new BaseMsgTemplate();
		tem.setTemplateId(templateId);
		tem.setTaskName(taskName);
		tem.setTitle(title); 
		tem.setContent(content);
		tem.setUri(uri);
		tem.setDeliveryType(Integer.parseInt(deliveryType));
		tem.setExpiredSeconds(expiredSeconds);
		tem.setTargetMsgkey(targetMsgkey);
		tem.setMsgType(msgType);
		tem.setShowMode(showMode);
		tem.setPushTag(pushTag);
		tem.setPushObject(pushObject);
		tem.setRemark(remark);
		
		Date date =  DateUtils.getConversionDateByLong(Long.parseLong(pushDate));
		tem.setPushDate(date);
		
		int i = baseMsgTemplateMapper.insertSelective(tem);
		if (i < 1 ) {
			result.put("success", false);
			result.put("info", "添加失败");
			result.put("templateId", "");
		}
		result.put("templateId", templateId);
		return result;
	}

}
